const md5 = require("md5");
const { login } = require("../../models/user");

// 用户登录
const Login = async (req, res) => {
    const { username, password } = req.body;
    const data = await login(username, md5(password));
    if (data) {
        req.session.user = {
            username: data.username,
        };
        res.send({
            code: 200,
            msg: "登录成功",
            data,
        });
    } else {
        res.send({
            code: 400,
            msg: "用户名或密码错误",
            data: null,
        });
    }
};

// 用户退出
const Logout = async (req, res) => {
    await req.session.destroy();
    res.send({
        code: 200,
        msg: "退出成功",
        data: null,
    });
};

module.exports = { Login, Logout };
